package com.yy.yyui.slider
{
	import com.siku.manor.config.Sys;
	import com.siku.manor.view.common.UM;
	import com.siku.manor.view.component.GT;
	import com.siku.manor.view.component.IActionList;
	import com.siku.manor.view.component.IDataContainer;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;

	public class Slider extends Sprite implements IDataContainer,IActionList
	{
		private var w:Number,h:Number;
		private var _data:Object;
		private var _actions:Object;
		private var bar:InteractiveObject;
		private var tracker:DisplayObject;		
		public function Slider(containerWidth:Number)
		{
			super();
			this.w = containerWidth;
			bar = new SliderBarSkin();
			bar.filters = [new BlurFilter(1.25,1.25)];
			this.h = bar.height;
			tracker = this.addChild(UM.createSliderTracker(this.width-bar.width));
			tracker.x = (this.width-tracker.width)/2;
			tracker.y = (this.height-tracker.height)/3;
			this.addChild(bar);
			GT.fulFill2(this,0,0);
			
			//set actions		
			this._actions = {};
			this.actions.onChange = null;
			bar.addEventListener(MouseEvent.MOUSE_DOWN,onClickBar);
		}
		
		public function set onChange(value:Function):void{this.actions.onChange = value;}
		public function get onChange():Function{return this.actions.onChange;}
		
		private var oldX:Number,oldY:Number;
		private var barX:Number;
		private function onClickBar(e:MouseEvent):void{
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			this.bar.transform.colorTransform = new ColorTransform(0.6,0.6,0.6);
			barX = e.localX;
			oldX = e.stageX;
			oldY = e.stageY;
			
		}
		
		private function onMouseMove(e:MouseEvent):void{
			var sx:Number = e.stageX;
			var sy:Number = e.stageY;
			var diffX:Number = sx-oldX;
			var rect:Rectangle = this.getRect(this.stage);
			var oldValue:Number = this.value;
			this.bar.x = sx-rect.x-barX;
			if(this.bar.x<0){
				this.bar.x = 0;
			}else if((this.bar.x+this.bar.width)>this.width){
				this.bar.x = this.width-this.bar.width;
			}
			var newValue:Number = this.value;
			this.onChange(oldValue,newValue);
			oldX=sx;
			oldY=sy;
			e.updateAfterEvent();
		}
		
		public function set enabled(value:Boolean):void{
			this.mouseEnabled = value;
			this.mouseChildren = value;
			if(!value){
				this.filters = Sys.COMPONENT_DISABLE_FILTER;
			}else{
				this.filters = null;
			}
		}
		
		public function get enabled():Boolean{
			return this.mouseEnabled;
		}
		
		public function set value(newValue:Number):void{
			if(newValue<0){
				newValue = 0;
			}else if(newValue>1){
				newValue = 1;
			}
			this.bar.x = this.tracker.width*newValue;
		}
		
		public function get value():Number{
			return this.bar.x/this.tracker.width; 
		}
		
		private function onMouseUp(e:MouseEvent):void{
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			this.bar.transform.colorTransform = new ColorTransform();
		}
		

		public function get actions():Object{return this._actions;}		
		public function get data():Object{return _data;}
		public function set data(value:Object):void{this._data = value;}
		
		override public function get width():Number{return w;}
		override public function get height():Number{return h;}
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			w = newWidth;
		}
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			h = newHeight;			
		}	
		
	}
}